package com.dog.modules.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dog.framework.core.constant.Constant;
import com.dog.framework.core.exception.ServiceException;
import com.dog.framework.core.utils.TreeUtils;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.modules.system.convert.SysOrgConvert;
import com.dog.modules.system.domain.SysOrg;
import com.dog.modules.system.domain.SysUser;
import com.dog.modules.system.api.vo.SysOrgVO;
import com.dog.modules.system.mapper.SysOrgMapper;
import com.dog.modules.system.mapper.SysUserMapper;
import com.dog.modules.system.service.SysOrgService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxk
 * @description 针对表【sys_org(机构管理)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
@AllArgsConstructor
public class SysOrgServiceImpl extends BaseServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService {

    private final SysUserMapper sysUserMapper;

    @Override
    public List<SysOrgVO> getList() {
        Map<String, Object> params = new HashMap<>();

        // 数据权限
        params.put(Constant.DATA_SCOPE, getDataScope("t1", "id"));

        // 机构列表
        List<SysOrg> entityList = baseMapper.getList(params);

        return TreeUtils.build(SysOrgConvert.INSTANCE.convertList(entityList));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysOrgVO vo) {
        SysOrg entity = SysOrgConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysOrgVO vo) {
        SysOrg entity = SysOrgConvert.INSTANCE.convert(vo);

        // 上级机构不能为自身
        if (entity.getId().equals(entity.getPid())) {
            throw new ServiceException("上级机构不能为自身");
        }

        // 上级机构不能为下级
        List<Long> subOrgList = getSubOrgIdList(entity.getId());
        if (subOrgList.contains(entity.getPid())) {
            throw new ServiceException("上级机构不能为下级");
        }

        updateById(entity);
    }

    @Override
    public void delete(Long id) {
        // 判断是否有子机构
        long orgCount = count(new QueryWrapper<SysOrg>().eq("pid", id));
        if (orgCount > 0) {
            throw new ServiceException("请先删除子机构");
        }

        // 判断机构下面是否有用户
        long userCount = sysUserMapper.selectCount(new QueryWrapper<SysUser>().eq("org_id", id));
        if (userCount > 0) {
            throw new ServiceException("机构下面有用户，不能删除");
        }

        // 删除
        removeById(id);
    }

    @Override
    public List<Long> getSubOrgIdList(Long id) {
        // 所有机构的id、pid列表
        List<SysOrg> orgList = baseMapper.getIdAndPidList();

        // 递归查询所有子机构ID列表
        List<Long> subIdList = new ArrayList<>();
        getTree(id, orgList, subIdList);

        // 本机构也添加进去
        subIdList.add(id);

        return subIdList;
    }

    private void getTree(Long id, List<SysOrg> orgList, List<Long> subIdList) {
        for (SysOrg org : orgList) {
            if (ObjectUtil.equals(org.getPid(), id)) {
                getTree(org.getId(), orgList, subIdList);
                subIdList.add(org.getId());
            }
        }
    }
}




